<?php

namespace App\Services\Admin;

use App\Services\Service;
use App\Models\Dict;

class DictService extends Service
{

    public static function index(array $params): array
    {

        $query = Dict::query();

        $pageNum = $params['pageNum'];
        $pageSize = $params['pageSize'];

        $paginator = $query->paginate($pageSize, ['*'], 'page', $pageNum);

        $paginator->getCollection()->makeVisible(['createTime', 'updateTime']);

        return [
            'list'  => $paginator->items(),
            'total' => $paginator->total(),
        ];
    }


    public static function store(array $data): array
    {

        $dict = Dict::create($data);

        return [
            'id' => $dict->id,
        ];
    }

    public static function show(string $id): array
    {

        $dict = Dict::find($id);

        if (!$dict) {
            self::throwValidationException('id', ['字典不存在']);
        }

        return $dict->toArray();
    }


    public static function update(array $data, string $id): array
    {

        $dict = Dict::find($id);

        if (!$dict) {
            self::throwValidationException('id', ['字典不存在']);
        }

        $dict->update($data);

        return [
            'id' => $dict->id,
        ];
    }

    public static function destroy(string $id): array
    {

        $dict = Dict::find($id);
        if (!$dict) {
            self::throwValidationException('id', ['字典不存在']);
        }

        $dict->delete();

        return [
            'id' => $dict->id
        ];
    }
}
